Add a search engine which queries the Spotlight database on MacOS X (only
authorKristian Rietveld <kris@gtk.org>
Sun, 24 Jun 2007 11:57:07 +0000 (11:57 +0000)
committerKristian Rietveld <kristian@src.gnome.org>
Sun, 24 Jun 2007 11:57:07 +0000 (11:57 +0000)
2007-06-24  Kristian Rietveld  <kris@gtk.org>

* gtk/gtksearchenginequartz.[ch]: Add a search engine which queries
the Spotlight database on MacOS X (only available in 10.4 and
higher).

* gtk/gtksearchengine.c (_gtk_search_engine_new): try creating
quartz search engine if we are on OS X.

* Makefile.am: added use_quartz_sources section with new file.

svn path=/trunk/; revision=18222

ChangeLog
gtk/Makefile.am
gtk/gtksearchengine.c
gtk/gtksearchenginequartz.c [new file with mode: 0644]
gtk/gtksearchenginequartz.h [new file with mode: 0644]

index ea4bc9c1f1811b508fddfe26b3cfc7b04e0af16c..78805213d23996735f0533895f0793023593306e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-06-24  Kristian Rietveld  <kris@gtk.org>
+
+       * gtk/gtksearchenginequartz.[ch]: Add a search engine which queries
+       the Spotlight database on MacOS X (only available in 10.4 and
+       higher).
+
+       * gtk/gtksearchengine.c (_gtk_search_engine_new): try creating
+       quartz search engine if we are on OS X.
+
+       * Makefile.am: added use_quartz_sources section with new file.
+
 2007-06-22  Emmanuele Bassi  <ebassi@gnome.org>
 
        * gtk/gtksearchenginebeagle.c:
index 440e4cb70f146c3bf4a85fce0dc8cd5cd2520d78..2994e23d28caae717cbd41ee28221730014a6feb 100644 (file)
@@ -660,10 +660,14 @@ gtk_use_win32_c_sources = \
        gtksocket-win32.c \
        gtkwin32embed.c   \
        gtkwin32embedwidget.c
+gtk_use_quartz_c_sources =     \
+       gtksearchenginequartz.c \
+       gtkplug-stub.c          \
+       gtksocket-stub.c
 gtk_use_stub_c_sources = \
        gtkplug-stub.c   \
        gtksocket-stub.c
-gtk_all_c_sources += $(gtk_use_x11_c_sources) $(gtk_use_win32_c_sources) $(gtk_use_stub_c_sources)
+gtk_all_c_sources += $(gtk_use_x11_c_sources) $(gtk_use_win32_c_sources) $(gtk_use_quartz_c_sources) $(gtk_use_stub_c_sources)
 if USE_X11
 gtk_private_h_sources += gtkxembed.h gtktrayicon.h xembed.h
 gtk_c_sources += $(gtk_use_x11_c_sources)
@@ -672,6 +676,11 @@ if USE_WIN32
 gtk_private_h_sources += gtkwin32embed.h gtkwin32embedwidget.h
 gtk_c_sources += $(gtk_use_win32_c_sources)
 else
+if USE_QUARTZ
+gtk_private_h_sources += gtksearchenginequartz.h
+gtk_c_sources += $(gtk_use_quartz_c_sources)
+gtk_use_quartz_c_sources_CFLAGS = "-xobjective-c"
+else
 gtk_c_sources += $(gtk_use_stub_c_sources)
 endif
 endif
index 85145d02e6abaf572dd79a926c3a7c3aa7987863..c1d23a4f55dd94ac0f5a9b22970960c6fcbd5628 100644 (file)
@@ -25,6 +25,9 @@
 #include "gtksearchenginebeagle.h"
 #include "gtksearchenginesimple.h"
 #include "gtksearchenginetracker.h"
+#include "gtksearchenginequartz.h"
+
+#include <gdk/gdkconfig.h> /* for GDK_WINDOWING_QUARTZ */
 
 #define HAVE_BEAGLE  1 
 #define HAVE_TRACKER 1
@@ -118,6 +121,12 @@ _gtk_search_engine_new (void)
     return engine;
 #endif
 
+#ifdef GDK_WINDOWING_QUARTZ
+  engine = _gtk_search_engine_quartz_new ();
+  if (engine)
+    return engine;
+#endif
+
   if (g_thread_supported ())
     engine = _gtk_search_engine_simple_new ();
   
diff --git a/gtk/gtksearchenginequartz.c b/gtk/gtksearchenginequartz.c
new file mode 100644 (file)
index 0000000..5d3d500
--- /dev/null
@@ -0,0 +1,274 @@
+/*
+ * Copyright (C) 2007  Kristian Rietveld  <kris@gtk.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ */
+
+#include <Cocoa/Cocoa.h>
+#include <quartz/gdkquartz.h>
+
+#include "gtksearchenginequartz.h"
+
+/* This file is a mixture of an objective-C object and a GObject,
+ * so be careful to not confuse yourself.
+ */
+
+#define QUARTZ_POOL_ALLOC NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]
+#define QUARTZ_POOL_RELEASE [pool release]
+
+
+/* Definition of objective-c object */
+@interface ResultReceiver : NSObject
+{
+  int submitted_hits;
+  GtkSearchEngine *engine;
+}
+
+- (void) setEngine:(GtkSearchEngine *)quartz_engine;
+
+- (void) queryUpdate:(id)sender;
+- (void) queryProgress:(id)sender;
+- (void) queryFinished:(id)sender;
+
+@end
+
+
+/* Definition of GObject */
+struct _GtkSearchEngineQuartzPrivate
+{
+  GtkQuery *query;
+
+  ResultReceiver *receiver;
+  NSMetadataQuery *ns_query;
+
+  gboolean query_finished;
+};
+
+G_DEFINE_TYPE (GtkSearchEngineQuartz, _gtk_search_engine_quartz, GTK_TYPE_SEARCH_ENGINE);
+
+
+/* Implementation of the objective-C object */
+@implementation ResultReceiver
+
+- (void) setEngine:(GtkSearchEngine *)quartz_engine
+{
+  g_return_if_fail (GTK_IS_SEARCH_ENGINE (quartz_engine));
+
+  engine = quartz_engine;
+  submitted_hits = 0;
+}
+
+- (void) submitHits:(NSMetadataQuery *)ns_query
+{
+  int i;
+  GList *hits = NULL;
+
+  /* Here we submit hits "submitted_hits" to "resultCount" */
+  for (i = submitted_hits; i < [ns_query resultCount]; i++)
+    {
+      id result = [ns_query resultAtIndex:i];
+      char *result_path;
+
+      result_path = g_strdup_printf ("file://%s", [[result valueForAttribute:@"kMDItemPath"] cString]);
+      hits = g_list_prepend (hits, result_path);
+    }
+
+  _gtk_search_engine_hits_added (engine, hits);
+  g_list_free (hits);
+
+  submitted_hits = [ns_query resultCount];
+
+  /* The beagle backend stops at 1000 hits, so guess we do so too here.
+   * It works pretty snappy on my MacBook, if we get rid of this limit
+   * we are almost definitely going to need some code to submit hits
+   * in batches.
+   */
+  if (submitted_hits > 1000)
+    [ns_query stopQuery];
+}
+
+- (void) queryUpdate:(id)sender
+{
+  NSMetadataQuery *ns_query = [sender object];
+
+  [self submitHits:ns_query];
+}
+
+- (void) queryProgress:(id)sender
+{
+  NSMetadataQuery *ns_query = [sender object];
+
+  [self submitHits:ns_query];
+}
+
+- (void) queryFinished:(id)sender
+{
+  NSMetadataQuery *ns_query = [sender object];
+
+  [self submitHits:ns_query];
+
+  _gtk_search_engine_finished (engine);
+  submitted_hits = 0;
+}
+
+@end
+
+/* Implementation of the GObject */
+
+static void
+gtk_search_engine_quartz_finalize (GObject *object)
+{
+  GtkSearchEngineQuartz *quartz;
+
+  QUARTZ_POOL_ALLOC;
+
+  quartz = GTK_SEARCH_ENGINE_QUARTZ (object);
+
+  [[NSNotificationCenter defaultCenter] removeObserver:quartz->priv->receiver];
+
+  [quartz->priv->ns_query release];
+  [quartz->priv->receiver release];
+
+  QUARTZ_POOL_RELEASE;
+
+  if (quartz->priv->query)
+    {
+      g_object_unref (quartz->priv->query);
+      quartz->priv->query = NULL;
+    }
+
+  G_OBJECT_CLASS (_gtk_search_engine_quartz_parent_class)->finalize (object);
+}
+
+static void
+gtk_search_engine_quartz_start (GtkSearchEngine *engine)
+{
+  GtkSearchEngineQuartz *quartz;
+
+  QUARTZ_POOL_ALLOC;
+
+  quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
+
+  [quartz->priv->ns_query startQuery];
+
+  QUARTZ_POOL_RELEASE;
+}
+
+static void
+gtk_search_engine_quartz_stop (GtkSearchEngine *engine)
+{
+  GtkSearchEngineQuartz *quartz;
+
+  QUARTZ_POOL_ALLOC;
+
+  quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
+
+  [quartz->priv->ns_query stopQuery];
+
+  QUARTZ_POOL_RELEASE;
+}
+
+static gboolean
+gtk_search_engine_quartz_is_indexed (GtkSearchEngine *engine)
+{
+  return TRUE;
+}
+
+static void
+gtk_search_engine_quartz_set_query (GtkSearchEngine *engine, 
+                                   GtkQuery        *query)
+{
+  GtkSearchEngineQuartz *quartz;
+
+  QUARTZ_POOL_ALLOC;
+
+  quartz = GTK_SEARCH_ENGINE_QUARTZ (engine);
+
+  if (query)
+    g_object_ref (query);
+
+  if (quartz->priv->query)
+    g_object_unref (quartz->priv->query);
+
+  quartz->priv->query = query;
+
+  /* We create a query to look for ".*text.*" in the text contents of
+   * all indexed files.  (Should we also search for text in file and folder
+   * names?).
+   */
+  [quartz->priv->ns_query setPredicate:
+    [NSPredicate predicateWithFormat:
+      [NSString stringWithFormat:@"(kMDItemTextContent LIKE[cd] \"*%s*\")",
+                                 _gtk_query_get_text (query)]]];
+
+  QUARTZ_POOL_RELEASE;
+}
+
+static void
+_gtk_search_engine_quartz_class_init (GtkSearchEngineQuartzClass *class)
+{
+  GObjectClass *gobject_class;
+  GtkSearchEngineClass *engine_class;
+  
+  gobject_class = G_OBJECT_CLASS (class);
+  gobject_class->finalize = gtk_search_engine_quartz_finalize;
+  
+  engine_class = GTK_SEARCH_ENGINE_CLASS (class);
+  engine_class->set_query = gtk_search_engine_quartz_set_query;
+  engine_class->start = gtk_search_engine_quartz_start;
+  engine_class->stop = gtk_search_engine_quartz_stop;
+  engine_class->is_indexed = gtk_search_engine_quartz_is_indexed;
+
+  g_type_class_add_private (gobject_class, sizeof (GtkSearchEngineQuartzPrivate));
+}
+
+static void
+_gtk_search_engine_quartz_init (GtkSearchEngineQuartz *engine)
+{
+  QUARTZ_POOL_ALLOC;
+
+  engine->priv = G_TYPE_INSTANCE_GET_PRIVATE (engine, GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartzPrivate);
+
+  engine->priv->ns_query = [[NSMetadataQuery alloc] init];
+  engine->priv->receiver = [[ResultReceiver alloc] init];
+
+  [engine->priv->receiver setEngine:GTK_SEARCH_ENGINE (engine)];
+
+  [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
+                                       selector:@selector(queryUpdate:)
+                                       name:@"NSMetadataQueryDidUpdateNotification"
+                                       object:engine->priv->ns_query];
+  [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
+                                       selector:@selector(queryFinished:)
+                                       name:@"NSMetadataQueryDidFinishGatheringNotification"
+                                       object:engine->priv->ns_query];
+
+  [[NSNotificationCenter defaultCenter] addObserver:engine->priv->receiver
+                                       selector:@selector(queryProgress:)
+                                       name:@"NSMetadataQueryGatheringProgressNotification"
+                                       object:engine->priv->ns_query];
+
+  QUARTZ_POOL_RELEASE;
+}
+
+GtkSearchEngine *
+_gtk_search_engine_quartz_new (void)
+{
+#ifdef GDK_WINDOWING_QUARTZ
+  return g_object_new (GTK_TYPE_SEARCH_ENGINE_QUARTZ, NULL);
+#else
+  return NULL;
+#endif
+}
diff --git a/gtk/gtksearchenginequartz.h b/gtk/gtksearchenginequartz.h
new file mode 100644 (file)
index 0000000..75f44e4
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2007  Kristian Rietveld  <kris@gtk.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ */
+
+#ifndef __GTK_SEARCH_ENGINE_QUARTZ_H__
+#define __GTK_SEARCH_ENGINE_QUARTZ_H__
+
+#include "gtksearchengine.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SEARCH_ENGINE_QUARTZ                  (_gtk_search_engine_quartz_get_type ())
+#define GTK_SEARCH_ENGINE_QUARTZ(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartz))
+#define GTK_SEARCH_ENGINE_QUARTZ_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartzClass))
+#define GTK_IS_SEARCH_ENGINE_QUARTZ(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SEARCH_ENGINE_QUARTZ))
+#define GTK_IS_SEARCH_ENGINE_QUARTZ_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_SEARCH_ENGINE_QUARTZ))
+#define GTK_SEARCH_ENGINE_QUARTZ_GET_CLASS(obj)                (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_SEARCH_ENGINE_QUARTZ, GtkSearchEngineQuartzClass))
+
+typedef struct _GtkSearchEngineQuartz GtkSearchEngineQuartz;
+typedef struct _GtkSearchEngineQuartzClass GtkSearchEngineQuartzClass;
+typedef struct _GtkSearchEngineQuartzPrivate GtkSearchEngineQuartzPrivate;
+
+struct _GtkSearchEngineQuartz
+{
+  GtkSearchEngine parent;
+
+  GtkSearchEngineQuartzPrivate *priv;
+};
+
+struct _GtkSearchEngineQuartzClass
+{
+  GtkSearchEngineClass parent_class;
+};
+
+GType            _gtk_search_engine_quartz_get_type (void);
+GtkSearchEngine *_gtk_search_engine_quartz_new      (void);
+
+G_END_DECLS
+
+#endif /* __GTK_SEARCH_ENGINE_QUARTZ_H__ */